package com.example.fourm.services.impl;

import com.example.fourm.common.AppResult;
import com.example.fourm.common.ResultCode;
import com.example.fourm.config.AppConfig;
import com.example.fourm.dao.ArticleMapper;
import com.example.fourm.exception.ApplicationException;
import com.example.fourm.model.Article;
import com.example.fourm.model.Board;
import com.example.fourm.model.User;
import com.example.fourm.services.IArticleService;
import com.example.fourm.services.IBoardService;
import com.example.fourm.services.IUserService;
import com.example.fourm.utils.StringUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private IUserService userService;
    @Resource
    private IBoardService boardService;


    @Override
    public void create(Article article) {
        if(article == null ||
            article.getBoardId() == null || StringUtil.isEmpty(article.getTitle()) == null
            || StringUtil.isEmpty(article.getContent()) == null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        //设置默认值
        Article newArticle = new Article();
         newArticle.setTitle(article.getTitle());
         newArticle.setContent(article.getContent());
         newArticle.setBoardId(article.getBoardId());
         newArticle.setUserId(article.getUserId());
         newArticle.setLikeCount(0);
         newArticle.setReplyCount(0);
         newArticle.setState((byte) 0);
         newArticle.setDeleteState((byte) 0);
        Date date = new Date();
        newArticle.setCreateTime(date);
        newArticle.setUpdateTime(date);
        //写入数据库
        int row = articleMapper.insertSelective(newArticle);
        if(row != 1){
            log.warn(ResultCode.FAILED.getMessage()+"新增文章失败！");
            throw new ApplicationException(ResultCode.FAILED.getMessage());
        }
        log.info("新增文章成功！！");

        //增加用户的文章数量
        Long userId = article.getUserId();
        User user = userService.selectByPrimaryKey(userId);
        if(user == null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
       userService.addOneArticleCountById(user.getId());
        log.info("用户文章数量增加成功");
        //增加板块的文章数量
        Long boardId = article.getBoardId();
        Board board = boardService.selectByPrimaryKey(boardId);
        if(board == null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage()+"板块不存在");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage()+"板块不存在");
        }
        boardService.addOneArticleCountById(board.getId());

        log.info("发帖成功");
    }

    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAll();
    }

    @Override
    public List<Article> selectAllByBoardId(Long boardId) {
        //非空校验
       if(boardId == null || boardId <= 0){
           log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
           throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
       }
        Board board = boardService.selectByPrimaryKey(boardId);
       //板块校验
       if(board == null){
           log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage()+"板块不存在");
           throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage()+"板块不存在");
       }
       return articleMapper.selectAllByBoardId(boardId);
    }

    @Override
    public Article selectDetailById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        Article article = articleMapper.selectDetailById(id);
        if(article == null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage()+" article为空");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage()+" article不存在" );
        }
        //article.getVisitCount();
        Article newArticle = new Article();
        newArticle.setId(article.getId());
        newArticle.setVisitCount(article.getVisitCount()+1);
        int row = articleMapper.updateByPrimaryKeySelective(newArticle);
        if(row != 1){
            log.warn(ResultCode.ERROR_SERVICES.getMessage()+" 访问次数增加操作失败！");
            throw new ApplicationException(ResultCode.ERROR_SERVICES.getMessage());
        }
        article.setVisitCount(article.getVisitCount()+1);
        return article;
    }

    @Override
    public Article selectById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void modify(Long id, String title, String content) {
        if(id == null || id <= 0 || StringUtil.isEmpty(title) || StringUtil.isEmpty(content)){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage() +" 编辑失败");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage() +"编辑失败");
        }
        Article newArticle = new Article();
        newArticle.setId(id);
        newArticle.setTitle(title);
        newArticle.setContent(content);
        newArticle.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(newArticle);
        if(row != 1){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage() +" 服务器异常");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage() +"编辑失败");
        }
    }

    @Override
    public void thumbsUpById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() != 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage()+" 该文章不存在!");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        if(article.getState() != 0 ){
            log.warn("文章状态异常");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        Article newArticle = new Article();
        newArticle.setId(article.getId());
        newArticle.setLikeCount(article.getLikeCount()+1);
        newArticle.setCreateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(newArticle);
        if(row != 1){
            log.warn("点赞失败！");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        log.info("点赞成功！！");
    }

    @Override
    public void deleteById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() != 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage()+" 该文章不存在!");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        if(article.getState() != 0 ){
            log.warn("文章状态异常");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        Article newArticle = new Article();
        newArticle.setId(article.getId());
        newArticle.setDeleteState((byte) 1);
        int row = articleMapper.updateByPrimaryKeySelective(newArticle);
        if(row != 1){
            log.warn("删除操作失败！！");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        boardService.subOneArticleCountById(article.getBoardId());
       userService.subOneArticleCountById(article.getUserId());
        log.info("删除操作成功！！");
    }

    @Override
    public void addOneReplyCountById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() != 0){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage()+" 该文章不存在!");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        if(article.getState() != 0 ){
            log.warn("文章状态异常");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        Article newArticle = new Article();
        newArticle.setId(article.getId());
        newArticle.setReplyCount(article.getReplyCount()+1);
        newArticle.setUpdateTime(new Date());
        int row = articleMapper.updateByPrimaryKeySelective(newArticle);
        if(row != 1){
            log.warn("回复失败！");
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.toString());
        }
        log.info("回复数增加成功！");
    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        if(userId == null || userId <= 0){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        List<Article> articles = articleMapper.selectByUserId(userId);
        return articles;
    }

}
